//
//  HistoryLiveTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/12/4.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "HistoryLiveTableViewCell.h"
#import <Masonry.h>
#import "NSString+Common.h"
#import "Live.h"

@implementation HistoryLiveTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self CreateUI];
    }
    return self;
}

- (void)CreateUI {
    
    _headPic = [[UIImageView alloc] initWithImage:IMAGE(@"placeHolder")];
    [self.contentView addSubview:_headPic];
    [_headPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(10);
//        make.width.and.height.mas_offset(SW(9));
        make.size.mas_equalTo(CGSizeMake(SW(9), SW(9)));
    }];
    _headPic.layer.cornerRadius = SW(18);
    _headPic.layer.masksToBounds = YES;
    
    _nickName = [[UILabel alloc] init];
    [self.contentView addSubview:_nickName];
    _nickName.text = @"nickName";
    _nickName.textColor = kBlackColor;
    _nickName.font = kUHSystemFontWithSize(14);
    [_nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_headPic.mas_centerY).mas_offset(0);
        make.left.mas_equalTo(_headPic.mas_right).mas_offset(10);
        make.height.mas_offset(SW(16));
    }];
    
    _date = [[UILabel alloc]init];
    _date.text = @"2017-12-04";
    _date.textColor = kLightGrayColor;
    _date.font = kUHSystemFontWithSize(12);
    [self.contentView addSubview:_date];
    [_date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_nickName.mas_centerY).mas_offset(0);
        make.right.mas_offset(-10);
        make.height.mas_offset(SW(18));
    }];
    
    
    _liveTitle = [[UILabel alloc]init];
    _liveTitle.text = @"我是标题";
    _liveTitle.textColor = kBlackColor;
    _liveTitle.font = kUHSystemFontWithSize(15);
    [self.contentView addSubview:_liveTitle];
    _liveTitle.numberOfLines = 2;
    [_liveTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPic.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
//        make.height.mas_offset(50);
    }];

    _cover = [[UIImageView alloc] initWithImage:IMAGE(@"placeHolder")];
    [self.contentView addSubview:_cover];
    [_cover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_liveTitle.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
//        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-20)/2, (SCREEN_HEIGHT-20)/2));
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-20)/2, (SCREEN_WIDTH-20)/2));
//        make.bottom.mas_offset(-10);
    }];
    
    _coverIcon = [[UIImageView alloc] initWithImage:IMAGE(@"icon_play")];
    [_cover addSubview:_coverIcon];
    [_coverIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(_cover);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
 
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_deleteBtn];
    [_deleteBtn setTitle:@" 删除 " forState:UIControlStateNormal];
    [_deleteBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = kUHSystemFontWithSize(15);
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_cover.mas_bottom).mas_offset(5);
        make.left.mas_offset(10);
//        make.size.mas_equalTo(CGSizeMake(SW(3), 44));
        make.bottom.mas_equalTo(self.mas_bottom).mas_offset(-10);
        make.size.mas_equalTo(CGSizeMake([LZCTools textSizeWithText:_deleteBtn.titleLabel.text font:_deleteBtn.titleLabel.font].width, [LZCTools textSizeWithText:_deleteBtn.titleLabel.text font:_deleteBtn.titleLabel.font].height));
    }];
    
}

- (void)setModel:(Live *)model {
    if (_model != model) {
        _model = model;
        
        [_headPic sd_setImageWithURL:[NSURL URLWithString:[model.anchor valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        _nickName.text = [model.anchor valueForKey:@"nickname"];
        
        _date.text = [NSString timeWithTimeIntervalString:model.start_time];
        
        _liveTitle.text = model.title;
        
        [_cover sd_setImageWithURL:[NSURL URLWithString:[model.anchor valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        
        if ([[model.anchor valueForKey:@"user_id"] isEqualToString:[GetUserDefaule(USERINFO) valueForKey:@"user_id"]]) {
            [_cover mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_liveTitle.mas_bottom).mas_offset(10);
                make.left.mas_offset(10);
                //        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-20)/2, (SCREEN_HEIGHT-20)/2));
                make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-20)/2, (SCREEN_WIDTH-20)/2));
            }];
            _deleteBtn.hidden = NO;
            [_deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_cover.mas_bottom).mas_offset(5);
                make.left.mas_offset(10);
                //        make.size.mas_equalTo(CGSizeMake(SW(3), 44));
                make.bottom.mas_offset(-10);
                make.size.mas_equalTo(CGSizeMake([LZCTools textSizeWithText:_deleteBtn.titleLabel.text font:_deleteBtn.titleLabel.font].width, [LZCTools textSizeWithText:_deleteBtn.titleLabel.text font:_deleteBtn.titleLabel.font].height));
            }];
        }
        else {
            [_cover mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_liveTitle.mas_bottom).mas_offset(10);
                make.left.mas_offset(10);
                make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-20)/2, (SCREEN_WIDTH-20)/2));
                 make.bottom.mas_offset(-10);
            }];
            
            _deleteBtn.hidden = YES;
//            [_deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.top.mas_equalTo(_cover.mas_bottom).mas_offset(5);
//                make.left.mas_offset(10);
//                //        make.size.mas_equalTo(CGSizeMake(SW(3), 44));
//                make.bottom.mas_equalTo(self.mas_bottom).mas_offset(-10);
//                make.size.mas_equalTo(CGSizeMake([LZCTools textSizeWithText:_deleteBtn.titleLabel.text font:_deleteBtn.titleLabel.font].width, [LZCTools textSizeWithText:_deleteBtn.titleLabel.text font:_deleteBtn.titleLabel.font].height));
//            }];
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
